<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">

     <el-form-item label="商城:">
         <el-select v-model="queryParams.mallId" style="width: 100%" filterable clearable placeholder="请选择商城">
            <el-option v-for="soption in options.customers" :key="soption.id" :label="soption.customerName" :value="soption.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="店铺:">
        <el-select v-model="queryParams.storeId" style="width: 100%" filterable clearable placeholder="请选择店铺" >
          <el-option v-for="option in options.storeOptions" :key="option.id" :label="option.storeName" :value="option.id" />
        </el-select>
      </el-form-item>

      <el-form-item label="品牌:">
      <el-select v-model="queryParams.brandId" placeholder="请选择品牌" filterable clearable size="mini" >
         <el-option v-for="boption in options.brands" :key="boption.id" :label="boption.brandName" :value="boption.id"  />
       </el-select>
       </el-form-item>
     <el-form-item label="SPU:">
       <el-input v-model="queryParams.custGoodsId" size="small" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
     </el-form-item>
      <el-form-item label="SKU:">
        <el-input v-model="queryParams.custSkuNo" size="small" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="商品名:">
        <el-input v-model="queryParams.spuName" size="small" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
     <!-- <el-col :span="1.5">
        <el-button v-hasPermission="['pms:custGoodsMap:insert']" type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col> -->
     <!-- <el-col :span="1.5">
        <el-button v-hasPermission="['pms:custGoodsMap:update']" type="success" icon="el-icon-edit" size="mini" :disabled="isSingle" @click="handleEdit">修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button v-hasPermission="['pms:custGoodsMap:delete']" type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleRemove">删除</el-button>
      </el-col>
      <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />

    <!--  <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
   --> </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" highlight-current-row @selection-change="handleSelectionChange"  @sort-change="handleSortChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="匹配ID" width="115" align="center" prop="id" />
      <el-table-column label="查看" align="center" show-overflow-tooltip width="180">
      <template slot-scope="scope">
        <el-button type="text" @click="handlePriceView(scope.row)">查看商品价格</el-button>
      </template>
      </el-table-column>
      <el-table-column label="商城" width="125" align="center" prop="customerName" />
      <el-table-column label="店铺" align="center" width="105" prop="storeName" sortable/>
      <el-table-column label="品牌" align="center" prop="brandName" />
   <el-table-column label="商城SPU" width="115" align="center" prop="custGoodsId" sortable/>
   <el-table-column label="商城SKU" width="115" align="center" prop="custSkuNo" sortable />
       <el-table-column label="商品名称" width="325" sortable align="center" prop="spuName" />
    <!--  <el-table-column label="商品sku名" width="125" align="center" sortable prop="skuName" />
    -->   <!-- <el-table-column label="商品类型" align="center" prop="goodsType" /> -->
       <el-table-column label="厂家货号" align="center" width="115" prop="sn" />
       <el-table-column label="条形码" align="center" width="115" prop="barCode" />
      <el-table-column label="分类" align="center" width="115" prop="categoryName" />
      <el-table-column label="规格" align="center" prop="spec" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermission="['pms:custGoodsMap:update']" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
          <el-button v-hasPermission="['pms:custGoodsMap:delete']" size="mini" type="text" icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <!-- 分页 -->

    <!-- 添加/修改对话框 -->
  <!--  <el-dialog :title="custGoodsMapFormDialog.title" :visible.sync="custGoodsMapFormDialog.visible" :close-on-click-modal="false" :before-close="resetCustGoodsMapFormDialog" width="500px" append-to-body>
      <el-form ref="custGoodsMapForm" :model="custGoodsMapFormData" :rules="custGoodsMapFormRules" label-width="80px">
        <el-form-item label="" prop="skuId">
          <el-input v-model="custGoodsMapFormData.skuId" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="spuId">
          <el-input v-model="custGoodsMapFormData.spuId" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="groupId">
          <el-input v-model="custGoodsMapFormData.groupId" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="custGoodsId">
          <el-input v-model="custGoodsMapFormData.custGoodsId" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmCustGoodsMapFormDialog">确 定</el-button>
        <el-button @click="resetCustGoodsMapFormDialog">取 消</el-button>
      </div>
    </el-dialog> -->
  </div>
</template>

<script>
import CustGoodsMapApi from '@/api/pms/CustGoodsMapApi'
import CustomerApi from '@/api/basic/CustomerApi'
import BrandApi from '@/api/goods/BrandApi'
import StoreApi from '@/api/basic/StoreApi'
const defaultCustGoodsMapFormData = {
  skuId: null,
  spuId: null,
  groupId: null,
  goodsType: null,
  custGoodsId: null
}
export default {
  name: 'CustGoodsMapList',
  data() {
    return {
      options: {
        sysYnTypeOptions: this.$constant.sysYnTypeOptions,
        customers: [],
        brands: [],
        storeOptions: []
      },
      queryParams: {
        skuId: null,
        spuName: null,
        mallId: null,
        storeId: null,
        brandId: null,
        spuId: null,
        groupId: null,
        goodsType: null,
        custGoodsId: null,
        isValid: this.$constant.Y,
        sortProperty: null,
        sortDirection: null
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      custGoodsMapFormDialog: {
        title: '',
        visible: false
      },
      custGoodsMapFormData: Object.assign({}, defaultCustGoodsMapFormData),
      custGoodsMapFormRules: {
      },
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
      CustomerApi.list().then(response => {
        this.options.customers = response.data
      })
      StoreApi.list().then(response => {
        this.options.storeOptions = response.data
      })
      BrandApi.list().then(response => {
        this.options.brands = response.data
      })
    },
    handleAdd() {
     // this.openCustGoodsMapFormDialog()
      this.$router.push('/spu/form')
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return CustGoodsMapApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleEdit(row) {
      const id = row.spuId
      this.$router.push('/spu/form/'+id)
       //this.openCustGoodsMapFormDialog(id)
    },
    handleQuery() {
      this.loading = true
      CustGoodsMapApi.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleSortChange(sort) {
      if (sort.order) {
        this.queryParams.sortProperty = sort.prop
        this.queryParams.sortDirection = sort.order === 'ascending' ? 'asc' : 'desc'
      } else {
        this.queryParams.sortProperty = null
        this.queryParams.sortDirection = null
      }
      this.handleQuery()
    },
    handleExport() {
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return CustGoodsMapApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.data)
      })
    },
    openCustGoodsMapFormDialog(id) {
      this.custGoodsMapFormDialog.visible = true
      if (id) {
        this.custGoodsMapFormDialog.title = '客户货号和平台货号映射表-修改'
        CustGoodsMapApi.get(id).then(response => {
          this.custGoodsMapFormData = {
            ...this.custGoodsMapFormData,
            ...response.data
          }
        })
      } else {
        this.custGoodsMapFormDialog.title = '客户货号和平台货号映射表-添加'
      }
    },
    confirmCustGoodsMapFormDialog() {
      this.$refs['custGoodsMapForm'].validate(valid => {
        if (valid) {
          if (this.custGoodsMapFormData.id) {
            CustGoodsMapApi.edit(this.custGoodsMapFormData).then(response => {
              this.$message.success('修改成功')
              this.resetCustGoodsMapFormDialog()
              this.handleQuery()
            })
          } else {
            CustGoodsMapApi.add(this.custGoodsMapFormData).then(response => {
              this.$message.success('添加成功')
              this.resetCustGoodsMapFormDialog()
              this.handleQuery()
            })
          }
        }
      })
    },
    handlePriceView(row) {
      this.$router.push({ path: `/materialBuyPrice/materialBuyPriceSpuList/${row.custGoodsId}` })
    },
    resetCustGoodsMapFormDialog() {
      this.custGoodsMapFormDialog = {
        title: '',
        visible: false
      }
      this.custGoodsMapFormData = Object.assign({}, defaultCustGoodsMapFormData)
      this.resetForm('custGoodsMapForm')
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    resetQuery() {
      this.page.current = 1
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
